package services.impl;

import dao.BillMapper;
import dao.ProviderMapper;
import domain.Provider;
import org.springframework.stereotype.Service;
import services.ProviderService;
import utils.PageBean;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProviderServiceImpl implements ProviderService {
    @Resource
    private ProviderMapper providerMapper;
    @Resource
    private BillMapper billMapper;

    @Override
    public List<Provider> queryProviderByPage(String proCode, String proName, PageBean pageBean) {
        return providerMapper.queryAllByPage(proCode,proName,pageBean);
    }

    @Override
    public int totalCount() {
        return providerMapper.totalCount();
    }

    @Override
    public boolean addProvider(Provider provider) {
        boolean flag  = false;
        int row = providerMapper.addProvider(provider);
        if(row>0){
            flag = true;
        }
        return flag;
    }

    @Override
    public Provider queryProviderById(int id) {
        return providerMapper.queryProviderById(id);
    }

    @Override
    public int deleteProvider(int id) {
        Provider provider = null;
        int flag = 1699; //删除失败
         int row = billMapper.queryBillCountById(id);
         provider= providerMapper.queryProviderById(id);
         if(provider == null){
             flag = 0; //供应商不存在
         }else if(row>0){
             //供应商下有订单记录
             flag = row;
         }else{
             int r = providerMapper.deleteProvider(id);
             if(r > 0){
                 //删除成功
                 flag = 1698;
             }
         }
        return flag;
    }

    @Override
    public boolean modifyProvider(Provider provider) {
        boolean flag = false;
        int row = providerMapper.updateProvider(provider);
        if(row>0){ //修改成功
            flag = true;
        }
        return flag;
    }

    @Override
    public List<Provider> queryAll() {
        return providerMapper.queryAll();
    }
}
